/**
 * 首页
 * @author  Jello
 * @create  2018/11/02 17:24
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var token = "";
var curPage = 1;
var size = 6;
var array = new Array();
var spinner = new Spinner(opts);

$(function() {
	token = sessionStorage.getItem("user_token") == null ? "" : sessionStorage.getItem("user_token");

	//轮播图
	var mySwiper = new Swiper('.swiper-container', {
		initialSlide: 0,
		// 轮播图的方向，也可以是vertical方向
		direction: 'horizontal',
		//播放速度
		loop: true,
		observer: true,
		observeParents: true,
		// 自动播放时间
		autoplay: 2500,
		// 播放的速度
		speed: 300,
		// 如果需要分页器，即下面的小圆点
		pagination: '.swiper-pagination',
		// 这样，即使我们滑动之后， 定时器也不会被清除
		autoplayDisableOnInteraction: false,
		paginationClickable: true,
		paginationType: 'bullets',
	});

	//获取个人资料
	var param = {};
	param.token = token;

	getUserMaterial(param, function(data) {
		var user_url = data.user_url;
		var user_name = data.user_name;
		var work_status = data.workStatus;
		var role = data.role; //角色
		//头像
		if(user_url == "" || user_url == null) {
			$("#user_url").attr("src", "img/personal/headphoto.png");
		} else {
			$("#user_url").attr("src", user_url);
		}
		//昵称
		$("#nickname").html(user_name);
		$("#user_name").html(user_name);
		//工作状态
		var temp = "";
		if(work_status == 0) {
			temp = "待业";
		} else if(work_status == 1) {
			temp = "在职";
		} else {
			temp = "入职中";
		}
		$("#user_status").html(temp);
		sessionStorage.setItem("nickname", user_name);
		sessionStorage.setItem("role", role);
	});

	//关闭提示框
	$(".errcover").click(function() {
		$(document).unbind("scroll");
		$(".errorbox").hide();
	})

	//菜单栏切换
	$(".info").click(function() {
		$(".tab_nav").toggle();
		$("#menubar").attr("src", $(".tab_nav").is(":hidden") ? "img/personal/arrow_bottom.png" : "img/personal/arrow_top.png");
	});

	//查询热门招聘
	flipOverPage(curPage);

	//查询理想岗位及曾任职工作
	var param = {};
	param.token = token;
	queryMechanismList(param, function(mechanismList) {
		var obj = new Array();
		for(var i = 0; i < mechanismList.length; i++) {
			var html = '<div class="item" value="' + mechanismList[i].id + '"><div class="product"><span>' + mechanismList[i].mechanismTypeName + '</span><img src="img/common/white.png" /></div></div>';
			$(".all-goods").append(html);
			var workTypeList = mechanismList[i].workTypeList;
			for(var j = 0; j < workTypeList.length; j++) {
				array.push(workTypeList[j].mechanismTypeId + "-" + workTypeList[j].workTypeName);
			}
		}
	})

	$('.all-goods .item').hover(function() {
		$(this).addClass('active')
		var goodId = $(this).attr("value");
		var html = "";
		$.each(array, function(index, item) {
			if(goodId == item.split("-")[0]) {
				html += '<span onclick="hideLevelTab(' + item.split("-")[0] + ',\'' + item.split("-")[1] + '\')">' + item.split("-")[1] + '</span>';
			}
		});
		var _html = '<div class="product-wrap pos01"><div class="cf">' + html + '</div></div>';
		$(".item").append(_html);
		$(this).find('.product-wrap').show();
	}, function() {
		$(this).removeClass('active')
		$(this).find('.product-wrap').hide();
	});

	//根据关键字或公司查询
	$("#searchJob").click(function() {
		var companyName = $("#companyKey").val();
		sessionStorage.setItem("keyword", companyName);
		window.location.href = "applyjob.html";
	})
	$("#companyKey").keyup(function(event) {
		if(event.keyCode == 13) {
			var companyName = $("#companyKey").val();
			sessionStorage.setItem("keyword", companyName);
			window.location.href = "applyjob.html";
		}
	});

	//查看更多
	$("#morehot_btn").click(function() {
		window.location.href = "favorjob.html";
	})

	//客服中心
	$("#service_btn1").click(function() {
		window.open("tencent://message/?uin=1423335266&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn2").click(function() {
		window.open("tencent://message/?uin=2028048473&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn3").click(function() {
		window.open("tencent://message/?uin=3341118684&Site=lanpin123.com&Menu=yes");
	})

})

/**
 * 查询热门招聘
 */
function flipOverPage(curPage) {
	//查询热门招聘
	$.ajax({
		type: "get",
		url: serverUrl + "/workapi/get_work_list_api?userToken=" + token + "&page=" + curPage + "&size=" + size + "&type=1" + "&sortType=1" + "&status=1",
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		timeout: asktime,
		dataType: "json",
		crossDomain: true,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var resultObj = eval(data);
			$(".indexJob").empty();
			if(resultObj.code == 0 && resultObj.data.list.length != 0) {
				for(var i = 0; i < resultObj.data.list.length; i++) {
					var lendType = ""; //借支
					var workType = "";
					var money = "";
					var keyList = "";
					var reStatus = "none";
					var isApply = "";
					var workList = resultObj.data.list;

					if(workList[i].lendType == 1) {
						lendType = "可借支";
					}
					if(workList[i].postType == 1) {
						workType = "小时工";
						money = workList[i].workMoney + '元/小时';
					} else if(workList[i].postType == 0) {
						workType = "正式工";
						money = workList[i].wageRange + '元/月';
					}
					if(workList[i].status == 1) { //0 正在招工 1 已经招满
						recruitStatus1 = "block";
						recruitStatus2 = "none";
					} else {
						recruitStatus1 = "none";
						recruitStatus2 = "block";
					}
					if(workList[i].key == null) {
						keyList = "";
					} else {
						keyList = workList[i].key; //关键字
					}

					if(workList[i].reStatus == 1) {
						reStatus = "block";
					}

					if(workList[i].isApply == 0 && workList[i].isApply != null) {
						isApply = "block";
					} else {
						isApply = "none";
					}

					var index_html = '<li><div class="content_work" onclick="queryWorkDetail(' + workList[i].id + ',' + workList[i].lendType + ')"><div class="content_work_top">' +
						'<span class="post" style="color: #FF5454;display: ' + recruitStatus1 + '">已招满</span>' +
						'<span class="post" style="display: ' + recruitStatus2 + '">需' + workList[i].workTypeName + '' + workList[i].maxNumber + '人</span>' +
						'<span class="salary">' + money + '</span></div>' +
						'<div class="content_work_center"><span class="worktype">' + workType + '</span>' +
						'<span class="lend">' + lendType + '</span><span class="applynum">已报名' + workList[i].applyNumber + '人</span></div>' +
						'<div class="content_work_bottom1"><img src="' + workList[i].mechanismUrl + '" /><span class="title3" style="display:' + isApply + '">已报名</span></div><div class="content_work_bottom2">' +
						'<span class="companyname">' + workList[i].mechanismName + '</span><span class="key">' + keyList + '</span>' +
						'<div class="title7" style="display:' + reStatus + '"><span>返' + workList[i].reMoney + '</span></div><span class="score">' + workList[i].mechanismScore + '分</span>' +
						'</div></div>' +
						'</li>';
					$(".indexJob").append(index_html);
				}
			} else if(resultObj.code == 10002) {
				window.location.href = "login.html";
			} else { //没有热门招聘
				$(".indexJob").hide();
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			closeSpin(300);
			return;
		}
	});
}

/**
 * 查询招聘详情
 */
function queryWorkDetail(id, lend) {
	workId = id;
	sessionStorage.setItem("lendType", lend);
	window.location.href = "jobdetail.html?id=" + workId;
}

/**
 * 跳转求职页面
 */
function applyPage() {
	window.location.href = "applyjob.html";
}

/**
 * 跳转热门招聘
 */
function favorPage() {
	window.location.href = "favorjob.html";
}

/**
 * 根据行业和岗位查询求职招聘
 */
function hideLevelTab(mechanismType, mechanismName) {
	$(".industrycon2").hide();
	sessionStorage.setItem("industrykey", mechanismType);
	sessionStorage.setItem("postnamekey", mechanismName);
	window.location.href = "applyjob.html";
}